package com.example.webviewexample.provider;

import java.io.File;
import java.io.FileNotFoundException;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Binder;
import android.os.ParcelFileDescriptor;
import android.os.Process;

public class HtmlFileContentProvider extends ContentProvider {

    public static final String BASE_URI = 
            "content://com.example.webviewexample.provider.htmlfileprovider";

    @Override
    public boolean onCreate() {
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        return null;
    }

    @Override
    public String getType(Uri uri) {
        String mimetype = uri.getQuery();
        return mimetype == null ? "" : mimetype;
    }

    @Override
    public ParcelFileDescriptor openFile(Uri uri, String mode)
            throws FileNotFoundException {
        // android:exported="false" is broken in older releases so we have to
        // manually enforce the calling identity.
        if (Process.myUid() != Binder.getCallingUid()) {
            throw new SecurityException("Permission denied");
        }
        if (!"r".equals(mode)) {
            throw new IllegalArgumentException("Bad mode for " + uri + ":"
                    + mode);
        }
        String filename = uri.getPath();
        File file = new File(filename);
        if (file.isDirectory()) {
            return null;
        }

        return ParcelFileDescriptor.open(file,
                ParcelFileDescriptor.MODE_READ_ONLY);
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        return null;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) {
        return 0;
    }

}
